﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DataTableManage.cs" company="HelloWorld">
//   BY黄媛媛
// </copyright>
// <summary>
//   Defines the DataTableManage type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace WindowsFormsApp2.Service
{
    using System;
    using System.Data;

    using WindowsFormsApp2.Model;

    /// <summary>
    /// The data table manage.
    /// </summary>
    internal class DataTableManage
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DataTableManage"/> class.
        /// </summary>
        /// <param name="dt">
        /// The dataTable.
        /// </param>
        public DataTableManage(DataTable dt)
        {
            this.CourseDataTable = dt;
        }

        /// <summary>
        /// Gets or sets the course data table.
        /// </summary>
        public DataTable CourseDataTable { get; set; }

        /// <summary>
        /// The data table initialization.
        /// </summary>
        public void DataTableInit()
        {
            this.CourseDataTable.Columns.Add("节数/周数", typeof(string)); // 添加列集，下面都是
            this.CourseDataTable.Columns.Add("周一", typeof(string));
            this.CourseDataTable.Columns.Add("周二", typeof(string));
            this.CourseDataTable.Columns.Add("周三", typeof(string));
            this.CourseDataTable.Columns.Add("周四", typeof(string));
            this.CourseDataTable.Columns.Add("周五", typeof(string));
            this.CourseDataTable.Columns.Add("周六", typeof(string));
            this.CourseDataTable.Columns.Add("周日", typeof(string));

            for (var i = 0; i < 13; i++)
            {
                // 用循环添加13个行集
                var dr = this.CourseDataTable.NewRow();
                this.CourseDataTable.Rows.Add(dr);
            }

            for (var i = 1; i < 14; i++)
            {
                this.CourseDataTable.Rows[i - 1][0] = i.ToString(); // 填入节数
            }
        }

        /// <summary>
        /// The data table fill.
        /// </summary>
        public void DataTableFill()
        {
            for (var weekday = 1; weekday < 8; weekday++)
            {
                // 清空课程表
                for (var lesson = 0; lesson < 13; lesson++)
                {
                    this.CourseDataTable.Rows[lesson][weekday] = string.Empty;
                }
            }

            foreach (var c in Course.Courses)
            {
                Console.WriteLine(c);

                // 填充课程
                for (var i = 0; i < c.LastLesson; i++)
                {
                    this.CourseDataTable.Rows[c.BeginLesson - 1 + i][c.Weekday] = c.Name;
                }
            }
        }
    }
}
